export const crc16 = (buffer) => {
	let crc = 0xFFFF;
	for (let i = 0; i < buffer.length; i++) {
		crc ^= buffer[i];
		for (let j = 0; j < 8; j++) {
			if (crc & 0x0001) {
				crc = (crc >> 1) ^ 0xA001;
			} else {
				crc = crc >> 1;
			}
		}
	}
	// 返回低位在前，高位在后的2字节CRC
	return new Uint8Array([crc & 0xFF, (crc >> 8) & 0xFF]);
};

// 将16进制字符串转换为Uint8Array
export const hexStringToBytes = (hexString) => {
	hexString = hexString.replace(/\s/g, '');
	if (hexString.length % 2 !== 0) {
		throw new Error('Hex string must have even length');
	}
	const bytes = new Uint8Array(hexString.length / 2);
	for (let i = 0; i < bytes.length; i++) {
		bytes[i] = parseInt(hexString.substr(i * 2, 2), 16);
	}
	return bytes;
}

// 将Uint8Array转换为16进制字符串
export const bytesToHexString = (bytes) => {
	return Array.from(bytes)
		.map(b => b.toString(16).padStart(2, '0'))
		.join('');
};

export const crc16Hex = (hexString) => {
    let string = hexString.replace(/\s+/g, '');
	const buffer = hexStringToBytes(string);
	return crc16(buffer);
};
// 使用示例
// const data = hexStringToBytes('010300000002');
// const crc = crc16(data);
// console.log(bytesToHexString(crc)); // 输出：c40b
	
// *推荐 直接处理16进制字符串的便捷函数
// console.log(bytesToHexString(crc16Hex('010300000002'))); // 输出：c40b


// 某值除某值，转成32位浮点数，再转换成16进制字符串
export const divideToFloat32Hex = (dividend, divisor) => {
    // 执行除法运算
    const result = dividend / divisor;
    
    // 创建一个ArrayBuffer来存储32位浮点数
    const buffer = new ArrayBuffer(4);
    const floatView = new Float32Array(buffer);
    const uintView = new Uint32Array(buffer);    
    // 将结果存入Float32Array
    floatView[0] = result;    
    // 获取32位无符号整数表示
    const uint32 = uintView[0];    
    // 转换为16进制字符串，并确保是8位
    const hexString = uint32.toString(16).padStart(8, '0').toUpperCase();    
    // return {
    //     decimal: result,
    //     hex: '0x' + hexString,
    //     binary: getFloat32Binary(result)
    // };
    return hexString;
}

// 辅助函数：获取32位浮点数的二进制表示
// function getFloat32Binary(value) {
//     const buffer = new ArrayBuffer(4);
//     const floatView = new Float32Array(buffer);
//     const uintView = new Uint32Array(buffer);    
//     floatView[0] = value;
//     const uint32 = uintView[0];    
//     // 转换为32位二进制字符串
//     const binary = uint32.toString(2).padStart(32, '0');
    
//     // 格式化显示：符号位(1) + 指数位(8) + 尾数位(23)
//     return binary.substring(0, 1) + ' ' + 
//            binary.substring(1, 9) + ' ' + 
//            binary.substring(9);
// }

// 使用示例
// console.log(divideToFloat32Hex(1, 1));
// 输出: { decimal: 1, hex: "0x3F800000", binary: "0 01111111 00000000000000000000000" }
// console.log(divideToFloat32Hex(10, 3));
// 输出: { decimal: 3.3333332538604736, hex: "0x40555555", binary: "0 10000000 10101010101010101010101" }
// console.log(divideToFloat32Hex(-5, 2));
// 输出: { decimal: -2.5, hex: "0xC0200000", binary: "1 10000000 01000000000000000000000" }
// console.log(divideToFloat32Hex(0, 0));
// 输出: { decimal: NaN, hex: "0x7FC00000", binary: "0 11111111 10000000000000000000000" }
// console.log(divideToFloat32Hex(1, 0));
// 输出: { decimal: Infinity, hex: "0x7F800000", binary: "0 11111111 00000000000000000000000" }

// 数字转16进制字符串
export const numberToHex = (number) => {
    return (number & 0xFF).toString(16).padStart(2, '0')
}

// 延迟执行
export const delay = (ms)  =>{
    return new Promise(resolve => setTimeout(resolve, ms));
}